<?php


namespace App\Customer\Controller;

use App\Customer\Model\UserAuth as UserAuth;
use EchoPHP\Facades\Request;

use Exception;
use EchoPHP\Foundation\Exception\HttpException;

/**
 * 登录页面继承的Controller类
 * Class Controller
 * @package App\Customer\Controller;
 */
abstract class Controller extends BaseController {
    private $ehireUserInfo = [];

    /**
     * Controller constructor.
     */
    public function __construct() {
        $this->request = Request::instance();
        $this->chkLoginKey();
    }

    public function chkLoginKey() {
        try {
            $authToken = '';
            if ($this->request->header()->has('authToken')) {
                $authToken = $this->request->header()->get('authToken');
            } elseif ($this->request->query()->has('authToken')) {
                $authToken = $this->request->query()->get('authToken');
            } elseif ($this->request->request()->has('authToken')) {
                $authToken = $this->request->request()->get('authToken');
            }

            $userAuth = new UserAuth();
            list($loginInfo,$token) = $userAuth->checkLoginSession($authToken);
            //没有登录信息则需要重新登录
            if (!isset($loginInfo['hruid'])) {
                throw new HttpException(self::STATUS_TOKEN_EXPIRED, $this->getStatusMsg(self::STATUS_TOKEN_EXPIRED));
            }
            if(!empty($token)) {
                app()->addGlobalHeaders('token', $token);
            }
            $this->ehireUserInfo = $loginInfo;
            return true;
        } catch (Exception $e) {
            throw new HttpException(self::STATUS_TOKEN_EXPIRED, $this->getStatusMsg(self::STATUS_TOKEN_EXPIRED));
        }

        throw new HttpException(self::STATUS_TOKEN_EXPIRED, $this->getStatusMsg(self::STATUS_TOKEN_EXPIRED));
    }


    /**
     * @return array
     */
    public function getEhireUserInfo() {
        return $this->ehireUserInfo;
    }

    /**
     * @return array
     */
    public function getEhireAccessKey() {
        return $this->ehireUserInfo['accesskey'];
    }

    /**
     * @return mixed
     */
    public function getEhireRole() {
        return $this->ehireUserInfo['role'];
    }

    /**
     * @return array
     */
    public function getEhireCtmid() {
        return $this->ehireUserInfo['ctmid'];
    }

    /**
     * @return array
     */
    public function getEhireHruid() {
        return $this->ehireUserInfo['hruid'];
    }

    /**
     * @return array
     */
    public function getCustomerId() {
        return $this->ehireUserInfo['ctrpoid'];
    }

    /**
     * @return array
     */
    public function getSubIds() {
        return $this->ehireUserInfo['subids'];
    }
}